package com.emser.unc.client.view;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.DevicesPresenter;
import com.emser.unc.client.resources.GlobalResources;
import com.emser.unc.serializable.LocationDTO;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.ImageResourceCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;

public class DevicesView extends Composite implements DevicesPresenter.Display {
	private CellTable<LocationDTO> deviceTable;
	private Column<LocationDTO , String> showColumn;
	private VerticalPanel verticalPanel;
	private SimplePanel simplePanel;
	private final GlobalResources resource;
	private BusyIndicatorView busyIndicator = new BusyIndicatorView(CloudMobile.constants.msjWorking());;
	private SimplePager pager;
	private int heightSPanel;
	public DevicesView() {
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter()+"px");
		heightSPanel = CloudMobile.get().getHeightPanelCenter()- 100;
		resource = GWT.create(GlobalResources.class);
		initHPanelTitle();
		initScrollPanel();
	    initPager();
	    busyIndicator.hide();
	}
	
	private void initScrollPanel() {
		ScrollPanel sPanel = new ScrollPanel();
		sPanel.setSize("100%-1px", heightSPanel + "px");
		deviceTable= new CellTable<LocationDTO >();
	    initColumnsTable();
	    deviceTable.setSize("100%", "100%");
	    sPanel.setWidget(deviceTable);
	    verticalPanel.add(sPanel);
	    verticalPanel.setCellHorizontalAlignment(sPanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(sPanel, "100%");
	    verticalPanel.setCellHeight(sPanel, heightSPanel + "px");
		
	}

	private void initHPanelTitle() {
		SimplePanel simplePanel_1 = new SimplePanel();
	    simplePanel_1.setStyleName("gwt-SimplePanelM1");
	    verticalPanel.add(simplePanel_1);
	    simplePanel_1.setSize("100%", "38px");
	    verticalPanel.setCellHeight(simplePanel_1, "40px");
	    verticalPanel.setCellWidth(simplePanel_1, "100%");
		
	}

	private void initPager() {
		simplePanel = new SimplePanel();
	    verticalPanel.add(simplePanel);
	    verticalPanel.setCellVerticalAlignment(simplePanel, HasVerticalAlignment.ALIGN_MIDDLE);
	    simplePanel.setSize("400px", "40px");
	    verticalPanel.setCellHorizontalAlignment(simplePanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(simplePanel, "100%");
	    verticalPanel.setCellHeight(simplePanel, "40px");
	    pager = new SimplePager(TextLocation.CENTER){
	    	@Override
	    	public void setPageStart(int index) {
	    	  if (getDisplay() != null) {
	    	    Range range = getDisplay().getVisibleRange();
	    	    int pageSize = range.getLength();
	    	    index = Math.max(0, index);
	    	    if (index != range.getStart()) {
	    	      getDisplay().setVisibleRange(index, pageSize);
	    	    }
	    	  }
	    	}
	    };
	    pager.setStyleName("gwt-pager1");
	    pager.setStylePrimaryName("gwt-pager1");
	    pager.setRangeLimited(true);
	    pager.setDisplay(deviceTable);
	    pager.setSize("400px", "15px");
	    pager.setPageSize(10);
	    pager.setPage(0);
	    simplePanel.add(pager);
	}
	
	private void initColumnsTable() {
		Column<LocationDTO , ImageResource> imageColumn =
			  new Column<LocationDTO , ImageResource>(new ImageResourceCell()) {
			    @Override
			    public ImageResource getValue(LocationDTO  object) {
			      return resource.getImageSmartPhone();
			    }
			  };
		imageColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		deviceTable.addColumn(imageColumn, "");
		TextColumn<LocationDTO> serialNumberColumn = new TextColumn<LocationDTO>() {
			@Override
			public String getValue(LocationDTO  object) {
				return object.getSerialDevice();
			}
		};
		serialNumberColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		deviceTable.addColumn(serialNumberColumn, CloudMobile.constants.serialDevice());
		TextColumn<LocationDTO> firstNameSellerColumn = new TextColumn<LocationDTO>() {
			@Override
			public String getValue(LocationDTO  object) {
				return object.getFirstName();
			}
		};
		firstNameSellerColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		deviceTable.addColumn(firstNameSellerColumn, CloudMobile.constants.firstNameSeller());
		TextColumn<LocationDTO> lastNameSellerColumn = new TextColumn<LocationDTO>() {
			@Override
			public String getValue(LocationDTO  object) {
				return object.getLastName();
			}
		};
		lastNameSellerColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		deviceTable.addColumn(lastNameSellerColumn, CloudMobile.constants.lastNameSeller());
		showColumn = new Column<LocationDTO, String>(new ButtonCell()) {
			@Override
			public String getValue(LocationDTO object) {
				return CloudMobile.constants.show();
			}
		};
		showColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		deviceTable.addColumn(showColumn, "");
		
	}
	@Override
	public Widget asWidget() {
	    return this;
	}

	@Override
	public Column<LocationDTO, String> getShowColumn() {
		return showColumn;
	}

	@Override
	public void showBusyIndicator(){
		busyIndicator.show();
	}
	@Override
	public void hideBusyIndicator(){
		busyIndicator.hide();
	}

	@Override
	public HasData<LocationDTO> getTable() {
		return deviceTable;
	}

	@Override
	public void setPage(int page) {
		pager.setPage(page);
		
	}

	@Override
	public int getPage() {
		return pager.getPage();
	}

}
